package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Asignaturaxcomponente;
import com.lostreseditores.sabersai.model.dto.AsignaturaxcomponenteDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class AsignaturaxcomponenteView {
    private InputText txtAsigidn_Asignatura;
    private InputText txtCompciaidn_Componente;
    private InputText txtAsigxcompciaidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<AsignaturaxcomponenteDTO> data;
    private AsignaturaxcomponenteDTO selectedAsignaturaxcomponente;

    public AsignaturaxcomponenteView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            AsignaturaxcomponenteDTO asignaturaxcomponenteDTO = (AsignaturaxcomponenteDTO) e.getObject();

            if (txtAsigidn_Asignatura == null) {
                txtAsigidn_Asignatura = new InputText();
            }

            txtAsigidn_Asignatura.setValue(asignaturaxcomponenteDTO.getAsigidn_Asignatura());

            if (txtCompciaidn_Componente == null) {
                txtCompciaidn_Componente = new InputText();
            }

            txtCompciaidn_Componente.setValue(asignaturaxcomponenteDTO.getCompciaidn_Componente());

            if (txtAsigxcompciaidn == null) {
                txtAsigxcompciaidn = new InputText();
            }

            txtAsigxcompciaidn.setValue(asignaturaxcomponenteDTO.getAsigxcompciaidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtAsigidn_Asignatura != null) {
            txtAsigidn_Asignatura.setValue(null);
            txtAsigidn_Asignatura.setDisabled(true);
        }

        if (txtCompciaidn_Componente != null) {
            txtCompciaidn_Componente.setValue(null);
            txtCompciaidn_Componente.setDisabled(true);
        }

        if (txtAsigxcompciaidn != null) {
            txtAsigxcompciaidn.setValue(null);
            txtAsigxcompciaidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Asignaturaxcomponente entity = null;

        try {
            Integer asigxcompciaidn = new Integer(txtAsigxcompciaidn.getValue()
                                                                    .toString());
            entity = BusinessDelegatorView.getAsignaturaxcomponente(asigxcompciaidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtAsigidn_Asignatura.setDisabled(false);
            txtCompciaidn_Componente.setDisabled(false);
            txtAsigxcompciaidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtAsigidn_Asignatura.setValue(entity.getAsignatura().getAsigidn());
            txtAsigidn_Asignatura.setDisabled(false);
            txtCompciaidn_Componente.setValue(entity.getComponente()
                                                    .getCompciaidn());
            txtCompciaidn_Componente.setDisabled(false);
            txtAsigxcompciaidn.setValue(entity.getAsigxcompciaidn());
            txtAsigxcompciaidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveAsignaturaxcomponente(FacesUtils.checkInteger(
                    txtAsigxcompciaidn),
                FacesUtils.checkInteger(txtAsigidn_Asignatura),
                FacesUtils.checkInteger(txtCompciaidn_Componente));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteAsignaturaxcomponente(FacesUtils.checkInteger(
                    txtAsigxcompciaidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateAsignaturaxcomponente(FacesUtils.checkInteger(
                    txtAsigxcompciaidn),
                FacesUtils.checkInteger(txtAsigidn_Asignatura),
                FacesUtils.checkInteger(txtCompciaidn_Componente));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtAsigxcompciaidn == null) {
                txtAsigxcompciaidn = new InputText();
            }

            txtAsigxcompciaidn.setValue(selectedAsignaturaxcomponente.getAsigxcompciaidn());

            BusinessDelegatorView.deleteAsignaturaxcomponente(FacesUtils.checkInteger(
                    txtAsigxcompciaidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedAsignaturaxcomponente);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Integer asigxcompciaidn,
        Integer asigidn_Asignatura, Integer compciaidn_Componente)
        throws Exception {
        try {
            BusinessDelegatorView.updateAsignaturaxcomponente(asigxcompciaidn,
                asigidn_Asignatura, compciaidn_Componente);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("AsignaturaxcomponenteView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtAsigidn_Asignatura() {
        return txtAsigidn_Asignatura;
    }

    public void setTxtAsigidn_Asignatura(InputText txtAsigidn_Asignatura) {
        this.txtAsigidn_Asignatura = txtAsigidn_Asignatura;
    }

    public InputText getTxtCompciaidn_Componente() {
        return txtCompciaidn_Componente;
    }

    public void setTxtCompciaidn_Componente(InputText txtCompciaidn_Componente) {
        this.txtCompciaidn_Componente = txtCompciaidn_Componente;
    }

    public InputText getTxtAsigxcompciaidn() {
        return txtAsigxcompciaidn;
    }

    public void setTxtAsigxcompciaidn(InputText txtAsigxcompciaidn) {
        this.txtAsigxcompciaidn = txtAsigxcompciaidn;
    }

    public List<AsignaturaxcomponenteDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataAsignaturaxcomponente();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<AsignaturaxcomponenteDTO> asignaturaxcomponenteDTO) {
        this.data = asignaturaxcomponenteDTO;
    }

    public AsignaturaxcomponenteDTO getSelectedAsignaturaxcomponente() {
        return selectedAsignaturaxcomponente;
    }

    public void setSelectedAsignaturaxcomponente(
        AsignaturaxcomponenteDTO asignaturaxcomponente) {
        this.selectedAsignaturaxcomponente = asignaturaxcomponente;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
